﻿<%@ Page Language="VB" Debug="true" %>
<%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Sub Page_Load()
        Dim ConnectionString As String = ConfigurationSettings.AppSettings("strcnn")
        Dim strsql As String
        Dim myConnection As New OleDbConnection(ConnectionString)
        Dim cnn as New OleDbConnection(ConnectionString)
        Dim myCommand as New OleDbCommand
        Dim cmd as New OleDbCommand
        Dim myDataReader as OleDbDataReader
        Dim dr as OleDbDataReader
        myConnection.Open()
        cnn.Open()
        myCommand.Connection = myConnection
        cmd.Connection = cnn
    
    
        select(Request.Form("acao"))
        case "excluir":
            Dim primeiro as Boolean = true
            for each i as String in Request.Form
                if i <> "acao" and i <> "RazaoSocial" then
                    if primeiro then
                        strsql = "delete * from conta where ID=" & i
                        primeiro = false
                    else
                        strsql = strsql & " or ID=" & i
                    End if
                End if
            next
            if strsql <> "" then
                myCommand.CommandText = strsql
                myCommand.ExecuteNonQuery()
            End if
        case "inserir":
            strsql = "Insert INTO conta ( IDFabrica, Banco, Agencia, Conta ) Values (" & Request.Form("IDFabrica") & ", '" & Request.Form("Banco") & "', '" & Request.Form("Agencia") & "', '" & Request.Form("Conta") & "')"
            myCommand.CommandText = strsql
            myCommand.ExecuteNonQuery()
        case "alterar":
            strsql = "update conta set IDFabrica=" & Request.Form("IDFabrica") & ", Banco='" & Request.Form("Banco") & "', Agencia='" & Request.Form("Agencia") & "', Conta='" & Request.Form("Conta") & "' where id=" & Request.Form("id")
            myCommand.CommandText = strsql
            myCommand.ExecuteNonQuery()
        End select
    
        select(Request("acao"))
        case "exibir":
            strsql = "select conta.id, fabrica.razaosocial, fabrica.id, conta.banco, conta.agencia, conta.conta from fabrica, conta where conta.id=" & Request.QueryString("id") & " and fabrica.id=conta.idfabrica"
            myCommand.CommandText = strsql
            myDataReader = myCommand.ExecuteReader()
            myDataReader.Read()
            Painel.Text = "<h3>Exibir:</h3><table border='0' cellspacing='0' cellpadding='4'>"
            Painel.Text = Painel.Text & "<tr><td>Fábrica:</td><td><a href='fabrica.aspx?acao=exibir&id=" & myDataReader("fabrica.id") & "'>" & myDataReader("RazaoSocial") & "</a></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Banco:</td><td>" & myDataReader("banco") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Agência:</td><td>" & myDataReader("agencia") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Conta:</td><td>" & myDataReader("conta") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td><form method='post' action='contafabrica.aspx'><input type='hidden' name='acao' value='formalterar'><input type='hidden' name='id' value='" & myDataReader("conta.ID") & "'><input type='submit' value='Alterar'></form></td>"
            Painel.Text = Painel.Text & "<td><form action='contafabrica.aspx'><input type='submit' value='Cancelar'></form></td></tr>"
            Painel.Text = Painel.Text & "</table>"
            myDataReader.Close()
        case "formalterar":
            cmd.CommandText = "select id, razaosocial from fabrica order by razaosocial asc"
            dr  = cmd.ExecuteReader()
            strsql = "select * from conta where id=" & Request.Form("id")
            myCommand.CommandText = strsql
            myDataReader = myCommand.ExecuteReader()
            myDataReader.Read()
            Painel.Text = "<h3>Alterar:</h3><form method='post' action='contafabrica.aspx'><input type='hidden' name='acao' value='alterar'><input type='hidden' name='id' value='" & myDataReader("ID") & "'><table border='0' cellspacing='0' cellpadding='4'>"
            Painel.Text = Painel.Text & "<tr><td>Fábrica:</td>"
                Painel.Text = Painel.Text & "<td><select name='idfabrica'>"
                while dr.Read()
                    Painel.Text = Painel.Text & "<option value='" & dr("id") & "'" & iif(dr("id")=myDataReader("idfabrica"), "selected", "") & ">" & dr("razaosocial") & "</option>"
                End while
                Painel.Text = Painel.Text & "</select></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Banco:</td><td><input name='Banco' value='" & myDataReader("Banco") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Agência:</td><td><input name='Agencia' value='" & myDataReader("Agencia") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Conta:</td><td><input name='Conta' value='" & myDataReader("Conta") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td><input type='submit' value='Alterar'></td><td><input type='reset' value='Limpar'></td></tr>"
            Painel.Text = Painel.Text & "</table></form>"
            myDataReader.Close()
            dr.Close()
        case else:
            myCommand.CommandText = "select id, razaosocial from fabrica order by razaosocial asc"
            dr  = myCommand.ExecuteReader()
            Painel.Text = "<h3>Inserir:</h3><form method='post' action='contafabrica.aspx'><input type='hidden' name='acao' value='inserir'><table border='0' cellspacing='0' cellpadding='4'>"
            Painel.Text = Painel.Text & "<tr><td>Fábrica:</td>"
                Painel.Text = Painel.Text & "<td><select name='idfabrica'>"
                while dr.Read()
                    Painel.Text = Painel.Text & "<option value='" & dr("id") & "'>" & dr("razaosocial") & "</option>"
                End while
                Painel.Text = Painel.Text & "</select></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Banco:</td><td><input name='Banco'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Agência:</td><td><input name='Agencia'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Conta:</td><td><input name='Conta'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td><input type='submit' value='Inserir'></td><td><input type='reset' value='Limpar'></td></tr>"
            Painel.Text = Painel.Text & "</table></form>"
            dr.Close()
        End Select
    
        Dim filterValue As String = Request.Form("RazaoSocial")
        if Request("Acao") = "inserir" or Request("Acao") = "alterar" then filterValue = ""
        If filterValue = "" Then
            strsql = "select conta.ID, conta.banco, conta.agencia, conta.conta, fabrica.RazaoSocial from conta, fabrica where fabrica.id=conta.idfabrica order by fabrica.RazaoSocial, conta.banco, conta.agencia, conta.conta asc"
        Else
            strsql = "select conta.ID, conta.banco, conta.agencia, conta.conta, fabrica.RazaoSocial from conta, fabrica where fabrica.id=conta.idfabrica and fabrica.razaosocial like '" & filterValue & "%' order by fabrica.RazaoSocial, conta.banco, conta.agencia, conta.conta asc"
        End If
        myCommand.CommandText = strsql
        myDataReader = myCommand.ExecuteReader()
        Busca.Text = "<table width='100%' border='0' cellspacing='2' cellpadding='2'><tr bgcolor='#000080'><td><font color='#FFFFFF'><b>Fábrica</b></font></td><td><font color='#FFFFFF'><b>Banco</b></font></td><td><font color='#FFFFFF'><b>Agência</b></font></td><td><font color='#FFFFFF'><b>Conta</b></font></td><td></td></tr>"
        Dim cor as Boolean
        while myDataReader.Read()
            Busca.Text = Busca.Text & "<tr bgcolor='" & iif(cor, "#ADD8E6", "#E0FFFF") & "'><td><a href='contafabrica.aspx?acao=exibir&id=" & myDataReader("ID") & "'>" & myDataReader("RazaoSocial") & "</a></td><td>" & myDataReader("Banco") & "</td><td>" & myDataReader("Agencia") & "</td><td>" & myDataReader("conta") & "</td><td align='center'><input type='checkbox' name='" & myDataReader("ID") & "' value='true'></td></tr>"
            cor = not cor
        End while
        Busca.Text = Busca.Text & "</table>"
    
        myConnection.Close()
        cnn.Close()
    End Sub

</script>
<html>
<head>
    <script language="vbscript">
    sub confirma()
        if msgbox("Confirmar exclusão?", vbYesNo) = vbYes then document.lista.submit()
    end sub
</script>
</head>
<body style="FONT-FAMILY: arial">
    <table bordercolor="navajowhite" cellspacing="0" cellpadding="2" width="950" align="center" bgcolor="floralwhite">
        <tbody>
            <tr>
                <td valign="center" align="middle" bgcolor="#ffdead" colspan="2">
                    <h2>Conta de fábrica 
                    </h2>
                </td>
            </tr>
            <tr>
                <td valign="top" width="550">
                    <table border="0">
                        <tbody>
                            <tr>
                                <td>
                                    <h3>Pesquisar: 
                                    </h3>
                                    <form method="post">
                                        <table border="0">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        Fábrica:</td>
                                                    <td>
                                                        <input name="RazaoSocial" /></td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                    </td>
                                                    <td align="right">
                                                        <input type="submit" value="Pesquisar" /></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </form>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <form name="lista" method="post">
                                        <input type="hidden" value="excluir" name="acao" />
                                        <%="<input type='hidden' name='RazaoSocial' value='" & Request.Form("RazaoSocial") & "'>"%><asp:Label id="Busca" runat="server">Busca</asp:Label>
                                        <br />
                                        <input onclick="vbscript:confirma()" type="button" value="Excluir" />
                                    </form>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
                <td valign="top" width="400">
                    <asp:Label id="Painel" runat="server">Painel</asp:Label></td>
            </tr>
        </tbody>
    </table>
</body>
</html>
